package com.order.controller;

import com.order.common.Result;
import com.order.entity.OrderProgress;
import com.order.service.OrderProgressService;
import com.order.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单进度控制器
 */
@RestController
@RequestMapping("/order-progress")
@CrossOrigin
public class OrderProgressController {

    @Autowired
    private OrderProgressService orderProgressService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取订单进度列表
     */
    @GetMapping("/list/{orderId}")
    public Result<List<OrderProgress>> getProgressList(@PathVariable Long orderId) {
        try {
            List<OrderProgress> progressList = orderProgressService.getProgressByOrderId(orderId);
            return Result.success(progressList);
        } catch (Exception e) {
            return Result.error("获取订单进度失败");
        }
    }

    /**
     * 添加订单进度
     */
    @PostMapping("/add")
    public Result<String> addProgress(
            @RequestBody Map<String, Object> request,
            @RequestHeader("Authorization") String token) {
        
        try {
            // 去掉Bearer前缀
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            
            Long userId = jwtUtil.getUserIdFromToken(token);
            Long orderId = Long.valueOf(request.get("orderId").toString());
            String content = request.get("content").toString();
            Integer progressType = Integer.valueOf(request.get("progressType").toString());
            
            orderProgressService.addProgress(orderId, userId, content, progressType);
            return Result.success("进度添加成功");
        } catch (Exception e) {
            return Result.error("添加进度失败");
        }
    }
}